package com.bsg.upm.check;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsg.upm.constant.DictConstants;
import com.bsg.upm.constant.Messages;
import com.bsg.upm.entity.AreaEntity;
import com.bsg.upm.entity.ClusterEntity;
import com.bsg.upm.entity.HostEntity;
import com.bsg.upm.entity.SanEntity;
import com.bsg.upm.entity.TaskEntity;
import com.bsg.upm.exception.APIException;
import com.bsg.upm.service.HostService;
import com.bsg.upm.util.IpV4Utils;

/**
 * 主机业务处理检查
 * 
 * @author HCK
 *
 */
@Service
public class HostCheck extends BaseCheck {

	@Resource
	private HostService hostService;

	/**
	 * 主机注册时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkSave(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkSaveNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkSaveLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 主机编辑时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkUpdate(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkUpdateNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkUpdateLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 主机容器上限编辑时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkUpdateMaxContainer(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		String hostId = (String) paramMap.get("hostId");
		if (StringUtils.isBlank(hostId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "主机编码"));
			return chkRS;
		}

		HostEntity host = hostDao.get(hostId);
		// 主机存在性检查
		if (host == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "主机"));
			return chkRS;
		}

		// 主机状态检查
		if (!DictConstants.HOST_STATUS_DISABLE.equals(host.getStatus())) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该主机不是'停用'状态，无法启用");
			return chkRS;
		}

		// 容器上限非空和有效性检查
		Integer maxContainer = (Integer) paramMap.get("maxContainer");
		if (maxContainer == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "容器上限"));
			return chkRS;
		}
		if (maxContainer < 1 || maxContainer > 100) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.BETWEEN_MIN_AND_MAX, "容器上限", 1, 100));
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 主机入库时检查
	 * 
	 * @param host
	 *            主机对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkInput(HostEntity host) {
		CheckResult chkRS = new CheckResult();
		// 主机存在性检查
		if (host == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "主机"));
			return chkRS;
		}

		// 主机状态检查
		if (!DictConstants.HOST_STATUS_INPUT_NO.equals(host.getStatus())) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该主机不是'未入库'状态，无法入库");
			return chkRS;
		}

		// 主机任务状态检查(任务未结束不能执行其他任务)
		TaskEntity task = taskDao.getLatestTaskByObjId(host.getId());
		if (task != null && task.getEndDateTime() == null) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该主机任务尚未结束，无法入库");
			return chkRS;
		}

		return chkRS;
	}

	/**
	 * 主机出库时检查
	 * 
	 * @param host
	 *            主机对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkOutput(HostEntity host) throws APIException {
		CheckResult chkRS = new CheckResult();
		// 主机存在性检查
		if (host == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "主机"));
			return chkRS;
		}

		// 主机状态检查
		if (DictConstants.HOST_STATUS_ENABLE.equals(host.getStatus())) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("启用状态，无法出库");
			return chkRS;
		}

		// 主机资源检查
		String siteId = host.getCluster().getArea().getSite().getId();
		JSONObject hostJsonObj = hostService.getHostInfoFromMGM(siteId, host.getRelateId());
		JSONArray containerJsonArr = hostJsonObj.getJSONArray("containers");
		if (containerJsonArr != null && containerJsonArr.size() != 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该主机资源未释放，无法出库");
			return chkRS;
		}

		// 主机任务状态检查(任务未结束不能执行其他任务)
		TaskEntity task = taskDao.getLatestTaskByObjId(host.getId());
		if (task != null && task.getEndDateTime() == null) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该主机任务尚未结束，无法出库");
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 主机启用时检查
	 * 
	 * @param host
	 *            主机对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkEnable(HostEntity host) {
		CheckResult chkRS = new CheckResult();
		// 主机存在性检查
		if (host == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "主机"));
			return chkRS;
		}

		// 主机状态检查
		if (!DictConstants.HOST_STATUS_DISABLE.equals(host.getStatus())) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该主机不是'停用'状态，无法启用");
			return chkRS;
		}

		// 主机任务状态检查(任务未结束不能执行其他任务)
		TaskEntity task = taskDao.getLatestTaskByObjId(host.getId());
		if (task != null && task.getEndDateTime() == null) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该主机任务尚未结束，无法启用");
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 主机停用时检查
	 * 
	 * @param host
	 *            主机对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkDisable(HostEntity host) {
		CheckResult chkRS = new CheckResult();
		// 主机存在性检查
		if (host == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "主机"));
			return chkRS;
		}

		// 主机状态检查
		if (!DictConstants.HOST_STATUS_ENABLE.equals(host.getStatus())) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该主机不是'启用'状态，无法停用");
			return chkRS;
		}

		// 主机任务状态检查(任务未结束不能执行其他任务)
		TaskEntity task = taskDao.getLatestTaskByObjId(host.getId());
		if (task != null && task.getEndDateTime() == null) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该主机任务尚未结束，无法停用");
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 主机注销时检查
	 * 
	 * @param host
	 *            主机对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkRemove(HostEntity host) {
		CheckResult chkRS = new CheckResult();
		// 主机存在性检查
		if (host == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "主机"));
			return chkRS;
		}

		// 主机状态检查
		if (!DictConstants.HOST_STATUS_INPUT_NO.equals(host.getStatus())
				|| StringUtils.isNotBlank(host.getRelateId())) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该主机未出库，无法注销");
			return chkRS;
		}
		return chkRS;
	}

	@SuppressWarnings("unchecked")
	public CheckResult checkUpload(Map<String, Object> excelData, List<HostEntity> hosts, List<ClusterEntity> clusters,
			List<SanEntity> sans) {
		CheckResult chkRS = new CheckResult();
		// 表单数量检查
		if (excelData.size() != 1) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("Excel格式错误：表单数量必须为1");
			return chkRS;
		}
		for (Map.Entry<String, Object> entry : excelData.entrySet()) {
			List<Object> rows = (List<Object>) entry.getValue();
			if (rows.size() < 2) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg("Excel格式错误：表单无数据");
				return chkRS;
			}
			for (int i = 1; i < rows.size(); i++) {
				List<Object> cells = (List<Object>) rows.get(i);
				if (StringUtils.isBlank((String) cells.get(0))) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行所属区域不能为空");
					return chkRS;
				}

				if (StringUtils.isBlank((String) cells.get(1))) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行所属集群不能为空");
					return chkRS;
				}

				if (StringUtils.isBlank((String) cells.get(2))) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行主机名不能为空");
					return chkRS;
				}
				if (((String) cells.get(2)).length() > 64) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行主机名长度不能大于64");
					return chkRS;
				}

				if (StringUtils.isBlank((String) cells.get(3))) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行主机IP不能为空");
					return chkRS;
				}
				if (!IpV4Utils.ipV4Validate((String) cells.get(3))) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg(MessageFormat.format(Messages.FORMAT_NOT_ALLOWED, "主机IP"));
					return chkRS;
				}

				if (StringUtils.isBlank((String) cells.get(4))) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行登录账号不能为空");
					return chkRS;
				}
				if (((String) cells.get(4)).length() > 64) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行登录账号长度不能大于64");
					return chkRS;
				}

				if (StringUtils.isBlank((String) cells.get(5))) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行登录密码不能为空");
					return chkRS;
				}
				if (((String) cells.get(5)).length() > 32) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行登录密码长度不能大于32");
					return chkRS;
				}

				if (StringUtils.isBlank((String) cells.get(6))) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行容器限不能为空");
					return chkRS;
				}
				if (Integer.parseInt((String) cells.get(6)) < 1 || Integer.parseInt((String) cells.get(6)) > 100) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行容器上限介于0-100之间");
					return chkRS;
				}

				if (StringUtils.isBlank((String) cells.get(7))) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行机房名称不能为空");
					return chkRS;
				}
				if (((String) cells.get(7)).length() > 64) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行机房名称长度不能大于64");
					return chkRS;
				}

				if (StringUtils.isBlank((String) cells.get(8))) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行机位不能为空");
					return chkRS;
				}
				if (((String) cells.get(8)).length() > 64) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行机位名称长度不能大于64");
					return chkRS;
				}

				if (StringUtils.isBlank((String) cells.get(9)) && StringUtils.isBlank((String) cells.get(10))
						&& StringUtils.isBlank((String) cells.get(11))) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行HDD设备、SSD设备和SAN存储不能全部为空");
					return chkRS;
				}

				if (StringUtils.isNotBlank((String) cells.get(9))) {
					if (((String) cells.get(9)).length() > 64) {
						chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
						chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行HDD设备长度不能大于64");
						return chkRS;
					}
				}

				if (StringUtils.isNotBlank((String) cells.get(10))) {
					if (((String) cells.get(10)).length() > 64) {
						chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
						chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行SSD设备长度不能大于64");
						return chkRS;
					}
				}

				boolean clusterExist = false;
				for (ClusterEntity cluster : clusters) {
					if (cluster.getName().equals(cells.get(1)) && cluster.getArea().getName().equals(cells.get(0))) {
						clusterExist = true;
						break;
					}
				}
				if (!clusterExist) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行所属集群不存在");
					return chkRS;
				}

				for (int j = i + 1; j < rows.size(); j++) {
					if (((String) cells.get(2)).equals(((List<Object>) rows.get(j)).get(2))) {
						chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
						chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行和第" + (j + 1) + "主机名重复");
						return chkRS;
					}

					if (((String) cells.get(3)).equals(((List<Object>) rows.get(j)).get(3))) {
						chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
						chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行和第" + (j + 1) + "主机IP重复");
						return chkRS;
					}
				}

				for (HostEntity host : hosts) {
					if (host.getName().equals(cells.get(2))) {
						chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
						chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行主机名已存在");
						return chkRS;
					}

					if (host.getSshIp().equals(cells.get(3))) {
						chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
						chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行主机IP已存在");
						return chkRS;
					}
				}

				if (StringUtils.isNotBlank((String) cells.get(11))) {
					boolean sanExist = false;
					for (SanEntity san : sans) {
						if (san.getName().equals(cells.get(11))) {
							sanExist = true;
						}
					}
					if (!sanExist) {
						chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
						chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行SAN设备不存在");
						return chkRS;
					}
				}

				if (((String) cells.get(12)).length() > 256) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("Excel格式错误：第" + (i + 1) + "行描述长度不能大于256");
					return chkRS;
				}
			}
			break;
		}
		return chkRS;
	}

	/**
	 * 新增时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 集群非空检查
		String clusterId = (String) paramMap.get("cluster");
		if (StringUtils.isBlank(clusterId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "所属集群"));
			return chkRS;
		}

		// 主机名非空和长度检查
		String name = (String) paramMap.get("name");
		if (StringUtils.isBlank(name)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "主机名称"));
			return chkRS;
		}
		if (StringUtils.trim(name).length() > 64) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "主机名称", 64));
			return chkRS;
		}

		// 主机IP非空和有效性检查
		String sshIp = (String) paramMap.get("sshIp");
		if (StringUtils.isBlank(sshIp)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "主机IP"));
			return chkRS;
		}
		if (!IpV4Utils.ipV4Validate(sshIp)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.FORMAT_NOT_ALLOWED, "主机IP"));
			return chkRS;
		}

		// 用户名非空和长度检查
		String sshUser = (String) paramMap.get("sshUser");
		if (StringUtils.isBlank(sshUser)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "SSH用户名"));
			return chkRS;
		}
		if (StringUtils.trim(sshUser).length() > 64) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "SSH用户名", 64));
			return chkRS;
		}

		// 密码非空和长度检查
		String sshPassword = (String) paramMap.get("sshPassword");
		if (StringUtils.isBlank(sshPassword)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "SSH密码"));
			return chkRS;
		}
		if (StringUtils.trim(sshPassword).length() > 32) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "SSH密码", 32));
			return chkRS;
		}

		// 机房非空和长度检查
		String room = (String) paramMap.get("room");
		if (StringUtils.isBlank(room)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "机房"));
			return chkRS;
		}
		if (StringUtils.trim(room).length() > 64) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "机房", 64));
			return chkRS;
		}

		// 机位非空和长度检查
		String seat = (String) paramMap.get("seat");
		if (StringUtils.isBlank(seat)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "机位"));
			return chkRS;
		}
		if (StringUtils.trim(seat).length() > 64) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "机位", 64));
			return chkRS;
		}

		// HDD设备、SSD设备和SAN存储非空和长度检查
		String hddDev = (String) paramMap.get("hddDev");
		String ssdDev = (String) paramMap.get("ssdDev");
		String sanId = (String) paramMap.get("san");
		if (StringUtils.isBlank(hddDev) && StringUtils.isBlank(ssdDev) && StringUtils.isBlank(sanId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("HDD设备、SSD设备和SAN存储至少输入其中一项");
			return chkRS;
		}
		if (StringUtils.isNotBlank(hddDev) && StringUtils.trim(hddDev).length() > 64) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "HDD设备", 64));
			return chkRS;
		}
		if (StringUtils.isNotBlank(ssdDev) && StringUtils.trim(ssdDev).length() > 64) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "SSD设备", 64));
			return chkRS;
		}

		// 容器上限非空和有效性检查
		Integer maxContainer = (Integer) paramMap.get("maxContainer");
		if (maxContainer == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "容器上限"));
			return chkRS;
		}
		if (maxContainer < 1 || maxContainer > 100) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.BETWEEN_MIN_AND_MAX, "容器上限", 1, 100));
			return chkRS;
		}

		if (paramMap.containsKey("description")) {
			// 描述长度检查
			String description = (String) paramMap.get("description");
			if (StringUtils.trim(description).length() > 256) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "描述", 256));
				return chkRS;
			}
		}

		return chkRS;
	}

	/**
	 * 新增时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 集群存在性检查
		String clusterId = (String) paramMap.get("cluster");
		ClusterEntity cluster = clusterDao.get(clusterId);
		if (cluster == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "所属集群"));
			return chkRS;
		}

		// 集群状态检查
		if (!cluster.getEnabled()) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.DISABLED_NOT_SELECT, "所属集群"));
			return chkRS;
		}

		AreaEntity area = cluster.getArea();
		// 名称重复性检查
		String name = (String) paramMap.get("name");
		if (hostDao.countByAreaIdAndName(area.getId(), name) > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "区域下主机名称"));
			return chkRS;
		}

		// IP重复性检查
		String sshIp = (String) paramMap.get("sshIp");
		if (hostDao.countByAreaIdAndIp(area.getId(), sshIp) > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "区域下主机IP"));
			return chkRS;
		}

		// SAN 存在性检查
		String sanId = (String) paramMap.get("san");
		if (StringUtils.isNoneBlank(sanId)) {
			if (sanDao.get(sanId) == null) {
				chkRS.setStatus(HttpStatus.SC_GONE);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "SAN存储"));
			}
		}

		return chkRS;
	}

	/**
	 * 编辑时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 主机非空检查
		String hostId = (String) paramMap.get("hostId");
		if (StringUtils.isBlank(hostId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "主机编码"));
			return chkRS;
		}

		if (paramMap.containsKey("clusterId")) {
			// 集群非空检查
			String clusterId = (String) paramMap.get("clusterId");
			if (StringUtils.isBlank(clusterId)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "集群编码"));
				return chkRS;
			}
		}

		if (paramMap.containsKey("name")) {
			// 主机名非空和长度检查
			String name = (String) paramMap.get("name");
			if (StringUtils.isBlank(name)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "主机名称"));
				return chkRS;
			}
			if (StringUtils.trim(name).length() > 64) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "主机名称", 64));
				return chkRS;
			}
		}

		if (paramMap.containsKey("sshIp")) {
			// 主机IP非空和有效性检查
			String sshIp = (String) paramMap.get("sshIp");
			if (StringUtils.isBlank(sshIp)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "主机IP"));
				return chkRS;
			}
			if (!IpV4Utils.ipV4Validate(sshIp)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.FORMAT_NOT_ALLOWED, "主机IP"));
				return chkRS;
			}
		}

		if (paramMap.containsKey("sshUser")) {
			// 用户名非空和长度检查
			String sshUser = (String) paramMap.get("sshUser");
			if (StringUtils.isBlank(sshUser)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "SSH用户名"));
				return chkRS;
			}
			if (StringUtils.trim(sshUser).length() > 64) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "SSH用户名", 64));
				return chkRS;
			}
		}

		if (paramMap.containsKey("sshPassword")) {
			// 密码非空和长度检查
			String sshPassword = (String) paramMap.get("sshPassword");
			if (StringUtils.isBlank(sshPassword)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "SSH密码"));
				return chkRS;
			}
			if (StringUtils.trim(sshPassword).length() > 32) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "SSH密码", 32));
				return chkRS;
			}
		}

		if (paramMap.containsKey("room")) {
			// 机房非空和长度检查
			String room = (String) paramMap.get("room");
			if (StringUtils.isBlank(room)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "机房"));
				return chkRS;
			}
			if (StringUtils.trim(room).length() > 64) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "机房", 64));
				return chkRS;
			}
		}

		if (paramMap.containsKey("seat")) {
			// 机位非空和长度检查
			String seat = (String) paramMap.get("seat");
			if (StringUtils.isBlank(seat)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "机位"));
				return chkRS;
			}
			if (StringUtils.trim(seat).length() > 64) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "机位", 64));
				return chkRS;
			}
		}

		if (paramMap.containsKey("hddDev") || paramMap.containsKey("ssdDev") || paramMap.containsKey("san")) {
			// HDD设备、SSD设备和SAN存储非空和长度检查
			String hddDev = (String) paramMap.get("hddDev");
			String ssdDev = (String) paramMap.get("ssdDev");
			String sanId = (String) paramMap.get("san");
			if (StringUtils.isBlank(hddDev) && StringUtils.isBlank(ssdDev) && StringUtils.isBlank(sanId)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg("HDD设备、SSD设备和SAN存储至少输入其中一项");
				return chkRS;
			}
			if (StringUtils.isNotBlank(hddDev) && StringUtils.trim(hddDev).length() > 64) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "HDD设备", 64));
				return chkRS;
			}
			if (StringUtils.isNotBlank(ssdDev) && StringUtils.trim(ssdDev).length() > 64) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "SSD设备", 64));
				return chkRS;
			}
		}

		if (paramMap.containsKey("maxContainer")) {
			// 容器上限非空和有效性检查
			Integer maxContainer = (Integer) paramMap.get("maxContainer");
			if (maxContainer == null) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "容器上限"));
				return chkRS;
			}
			if (maxContainer < 1 || maxContainer > 100) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.BETWEEN_MIN_AND_MAX, "容器上限", 1, 100));
				return chkRS;
			}
		}

		if (paramMap.containsKey("description")) {
			// 描述长度检查
			String description = (String) paramMap.get("description");
			if (StringUtils.trim(description).length() > 256) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "描述", 256));
				return chkRS;
			}
		}

		return chkRS;
	}

	/**
	 * 编辑时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();
		// 主机存在性检查
		String hostId = (String) paramMap.get("hostId");
		HostEntity host = hostDao.get(hostId);
		if (host == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "主机"));
			return chkRS;
		}

		// 主机状态检查
		if (!DictConstants.HOST_STATUS_INPUT_NO.equals(host.getStatus())) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该主机未出库，无法编辑");
			return chkRS;
		}

		if (StringUtils.isNotBlank(host.getRelateId())) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该主机未成功出库，无法编辑");
			return chkRS;
		}

		ClusterEntity cluster = host.getCluster();
		if (paramMap.containsKey("cluster")) {
			String clusterId = (String) paramMap.get("cluster");
			if (!cluster.getId().equals(clusterId)) {
				// 集群存在性检查
				ClusterEntity newCluster = clusterDao.get(clusterId);
				if (newCluster == null) {
					chkRS.setStatus(HttpStatus.SC_GONE);
					chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "集群"));
					return chkRS;
				}

				// 集群状态检查
				if (!newCluster.getEnabled()) {
					chkRS.setStatus(HttpStatus.SC_GONE);
					chkRS.setErrorMsg(MessageFormat.format(Messages.DISABLED_NOT_SELECT, "集群"));
					return chkRS;
				}
			}
		}

		String areaId = cluster.getArea().getId();
		if (paramMap.containsKey("name")) {
			String name = (String) paramMap.get("name");
			if (!name.equals(host.getName())) {
				// 名称重复性检查
				if (hostDao.countByAreaIdAndName(areaId, name) > 0) {
					chkRS.setStatus(HttpStatus.SC_CONFLICT);
					chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "区域下主机名称"));
					return chkRS;
				}
			}
		}

		if (paramMap.containsKey("sshIp")) {
			String sshIp = (String) paramMap.get("sshIp");
			if (!sshIp.equals(host.getSshIp())) {
				// 主机IP重复性检查
				if (hostDao.countByAreaIdAndIp(areaId, sshIp) > 0) {
					chkRS.setStatus(HttpStatus.SC_CONFLICT);
					chkRS.setErrorMsg(MessageFormat.format(Messages.EXIST, "区域下主机IP"));
					return chkRS;
				}
			}
		}

		if (paramMap.containsKey("san")) {
			String sanId = (String) paramMap.get("san");
			if (StringUtils.isNoneBlank(sanId)) {
				// SAN存在性检查
				if (sanDao.get(sanId) == null) {
					chkRS.setStatus(HttpStatus.SC_GONE);
					chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "SAN存储"));
				}
			}
		}
		return chkRS;
	}
}
